
// 获取应用实例
const app = getApp()
Page({
    data: {
        picPath: app.globalData.picPath,
        birthDay: '',//出生日期
        userType: '',// 1 男职工    2 女干部  3 女职工
        userTypeText: '',
        oldBackTime: '',// 原退休时间
        longMonthNum: 0,//延长月份
        newBackTime: '',//新退休时间
        backAge: '',// 退休年龄
        tipText: app.globalData.tipText,// 顶部提示语
        showBirthPop: false,// 选择生日弹窗
        minDate: new Date(1925, 0, 1).getTime(),
        maxDate: new Date().getTime(),
        currentDate: new Date().getTime(),
        showTypePop: false,//选择性别和人员类型弹窗
        actions: [
            {
                name: '男职工',
                value: '1',
            },
            {
                name: '原法定退休年龄55周岁女职工',
                value: '2',
            },
            {
                name: '原法定退休年龄50周岁女职工',
                value: '3',
            }],
        isPass: false,// 是否已经退休
        navList: [],//底部导航栏
        resultPop: false,// 测试结果
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
        var navList = app.globalData.navList, nowPage = '/' + this.is;
        navList = navList.filter(item => {
            return item.path != nowPage
        })
        this.setData({
            navList
        })
        console.log(this.data);

    },
    backHandle() {
        this.setData({
            resultPop: false
        })
    },
    toNav(e) {
        console.log(e);
        var path = e.currentTarget.dataset.nav.path;
        console.log(path);
        if (path == '') {
            wx.showToast({
                title: '抱歉，功能暂未开通',
                icon: 'none'
            })
            return
        }
        wx.redirectTo({
            url: path,
        })
    },
    // 查询
    checkHandle() {
        if (this.data.birthDay == '') {
            wx.showToast({
                title: '请选择出生年月',
                icon: 'none'
            })
            return
        }
        if (this.data.userTypeText == '') {
            wx.showToast({
                title: '请选择性别及人员类型',
                icon: 'none'
            })
            return
        }
        console.log(this.data.birthDay);
        console.log(this.data.userType);
        console.log(this.data.userTypeText);

        this.getData()

    },
    // 重置
    resHandle() {
        wx.showModal({
            title: '提示',
            content: '是否重置信息',
            complete: (res) => {
                if (res.cancel) {
                }
                if (res.confirm) {
                    this.setData({
                        birthDay: '',
                        userType: '',
                        userTypeText: '',
                    })
                }
            }
        })
    },

    // 选择生日
    chooseBirth() {
        console.log(1);
        this.setData({
            showBirthPop: true
        })
    },

    // 确认生日弹窗
    birthConfirm(event) {
        var time = event.detail
        var str = new Date(time).getFullYear() + '-' + this.add0(new Date(time).getMonth() + 1) + '-' + this.add0(new Date(time).getDate())
        console.log(str);
        this.setData({
            showBirthPop: false,
            birthDay: str,
        });
    },
    // 关闭生日弹窗
    birthCancel() {
        this.setData({
            showBirthPop: false
        })
    },


    // 显示选择类型弹窗
    chooseType() {
        this.setData({
            showTypePop: true
        })
    },
    // 关闭选择类型弹窗
    cancelHandle() {
        this.setData({
            showTypePop: false
        })
    },


    // 确认类型
    selectHandle(e) {
        console.log(e.detail);
        this.setData({
            userTypeText: e.detail.name,
            userType: e.detail.value,
            showTypePop: false
        })
    },

    getData() {
        this.setData({
            oldBackTime: this.getYuBackTime(this.data.birthDay, this.data.userType),
        })
        this.setData({
            longMonthNum: this.getMonthDifference(this.data.oldBackTime, this.data.userType),
        })
        this.setData({
            newBackTime: this.addMonthsToDate(this.data.oldBackTime, this.data.longMonthNum),
        })
        this.setData({
            backAge: this.getBackAge(this.data.longMonthNum, this.data.userType)
        })
        console.log(this.data);
        this.setData({
            resultPop: true
        })

    },
    // 计算新的退休日期
    addMonthsToDate(startDate, monthsToAdd) {
        // 确保传入的参数是Date对象
        startDate = new Date(startDate)
        if (!(startDate instanceof Date)) {
            throw new Error("Invalid date object");
        }

        // 创建一个新的Date对象以避免修改原始日期
        let newDate = new Date(startDate);

        // 获取当前月份
        let currentMonth = newDate.getMonth();

        // 计算新的月份和年份
        let newMonth = currentMonth + monthsToAdd;
        let newYear = newDate.getFullYear() + Math.floor(newMonth / 12);
        newMonth = newMonth % 12;

        // 设置新的月份和年份
        newDate.setFullYear(newYear);
        newDate.setMonth(newMonth);

        // 处理日期溢出问题（例如从1月31日加1个月）
        if (newDate.getMonth() !== newMonth % 12) {
            newDate.setDate(0); // 设置为上个月的最后一天
        }
        var timeStr = newDate
        return timeStr.getFullYear() + '-' + ((timeStr.getMonth() + 1) < 10 ? '0' + (timeStr.getMonth() + 1) : (timeStr.getMonth() + 1)) + '-' + ((timeStr.getDate()) < 10 ? '0' + (timeStr.getDate()) : (timeStr.getDate()));
    },
    // 计算退休年龄
    getBackAge(month, flag) {
        return month <= 0 ? ((flag == 1 ? 60 : flag == 2 ? 55 : 50)) : Math.ceil(((flag == 1 ? 60 : flag == 2 ? 55 : 50) + (Number(month) / 12)))
    },
    // 获取原退休时间
    getYuBackTime(birthDay, flag) {
        console.log(birthDay);
        // flag    1 男职工 60岁    2 女干部  55岁   3 女职工  50岁
        var monthAndDay = birthDay.split('-')[1] + '-' + birthDay.split('-')[2]
        birthDay = (new Date(birthDay))
        var year = birthDay.getFullYear() + (flag == 1 ? 60 : flag == 2 ? 55 : 50)
        var str = year + '-' + monthAndDay
        console.log(str);
        return str
    },
    // 获取相差多少月
    getMonthDifference(backTime, flag) {
        console.log(backTime);
        // // 确保传入的参数是Date对象
        var startDate = new Date('2025-01-01'), backTime = new Date(backTime)
        if (!(startDate instanceof Date) || !(backTime instanceof Date)) {
            throw new Error("Invalid date object");
        }
        // 计算年份和月份的差异
        let yearDifference = backTime.getFullYear() - startDate.getFullYear();
        let monthDifference = backTime.getMonth() - startDate.getMonth();
        // 计算总月数差异
        let totalMonthDifference = yearDifference * 12 + monthDifference;
        // 如果结束日期的天数小于开始日期的天数，则减去一个月
        if (backTime.getDate() < startDate.getDate()) {
            totalMonthDifference--;
        }

        this.setData({
            isPass: totalMonthDifference <= 0
        })
        console.log(totalMonthDifference);
        // 延迟
        var month = Math.ceil(totalMonthDifference / (flag == 3 ? 2 : 4))
        // console.log(totalMonthDifference);
        // 女职工最多延迟五年  60个月
        if (flag == 3 && month > 60) {
            month = 60
        }
        // 男职工 女干部 最多延迟3年  36个月
        if (flag != 3 && month > 36) {
            month = 36
        }
        return month;
    },
    getMonthNum(start, end) {
        let start_num = new Date(start.replace(/-/g, "/"))
        let end_num = new Date(end.replace(/-/g, "/"))
        let day = parseInt((end_num.getTime() - start_num.getTime()) / (1000 * 60 * 60 * 24))
        console.log(day);
    },


    add0(val) {
        return val < 10 ? '0' + val : val
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
    },
})